/*
 * Copyright (C) 2021 Huawei Device Co., Ltd.
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *     http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

package com.lahm.easyprotector.slice;

import ohos.aafwk.ability.AbilitySlice;
import ohos.aafwk.content.Intent;
import ohos.agp.components.Component;
import ohos.agp.components.Text;
import ohos.agp.window.service.WindowManager;

import com.lahm.easyprotector.BuildConfig;
import com.lahm.easyprotector.ResourceTable;
import com.lahm.library.Callback;
import com.lahm.library.EasyProtectorLib;
import com.lahm.library.EmulatorCheckCallback;
import com.lahm.library.SecurityCheckUtil;
import com.lahm.library.VirtualApkCheckUtil;
import com.lahm.library.utils.Log;

/**
 * * 因目前library下的SO库文件无法打包到HAR包里面，所以需要将SO文件手动导入到entry lib目录下才能找到SO库文件，每次更改CPP代码都需要导入一次并且clean项目。后续如果工具有升级可以免去手动导入SO库文件。
 * classpath com.huawei.ohos:hap:2.4.2.5
 * DevEco Studio 2.1 Beta 3
 *
 * @since 2021-04-07
 */
public class MainAbilitySlice extends AbilitySlice implements Component.ClickedListener {
    // Load the 'native-lib' library on application startup.
    private static final String TAG = "MainAbilitySlice";

    @Override
    public void onStart(Intent intent) {
        super.onStart(intent);
        super.setUIContent(ResourceTable.Layout_main_ability_slice);
        WindowManager.getInstance().getTopWindow()
            .get().setStatusBarColor(getColor(ResourceTable.Color_colorPrimaryDark));
        findComponentById(ResourceTable.Id_checkByPrivateFilePath).setClickedListener(this);
        findComponentById(ResourceTable.Id_checkByOriginApkPackageName).setClickedListener(this);
        findComponentById(ResourceTable.Id_checkByMultiApkPackageName).setClickedListener(this);
        findComponentById(ResourceTable.Id_checkByHasSameUid).setClickedListener(this);
        findComponentById(ResourceTable.Id_checkByPortListening).setClickedListener(this);
        findComponentById(ResourceTable.Id_checkByCreateLocalServerSocket).setClickedListener(this);
        findComponentById(ResourceTable.Id_checkRoot).setClickedListener(this);
        findComponentById(ResourceTable.Id_checkDebuggable).setClickedListener(this);
        findComponentById(ResourceTable.Id_checkDebuggerAttach).setClickedListener(this);
        findComponentById(ResourceTable.Id_checkTracer).setClickedListener(this);
        findComponentById(ResourceTable.Id_checkXP).setClickedListener(this);
        findComponentById(ResourceTable.Id_readSysProperty).setClickedListener(this);
        findComponentById(ResourceTable.Id_test).setClickedListener(this);
    }

    @Override
    public void onClick(Component component) {
        switch (component.getId()) {
            case ResourceTable.Id_test:
                break;
            case ResourceTable.Id_checkByPrivateFilePath:
                checkByPrivateFilePath();
                break;
            case ResourceTable.Id_checkByOriginApkPackageName:
                checkByOriginApkPackageName();
                break;
            case ResourceTable.Id_checkByMultiApkPackageName:
                checkByMultiApkPackageName();
                break;
            case ResourceTable.Id_checkByHasSameUid:
                checkByHasSameUid();
                break;
            case ResourceTable.Id_checkByPortListening:
                VirtualApkCheckUtil.getSingleInstance().checkByPortListening("port", null);
                Text v5 = (Text) findComponentById(ResourceTable.Id_v5);
                v5.setText("port listening");
                break;
            case ResourceTable.Id_checkByCreateLocalServerSocket:
                checkIsRunningInVirtualApk();
                break;
            case ResourceTable.Id_checkRoot:
                Text r1 = (Text) findComponentById(ResourceTable.Id_r1);
                r1.setText(EasyProtectorLib.checkIsRoot() ? "rooted" : "no-root");
                break;
            case ResourceTable.Id_checkDebuggable:
                Text d1 = (Text) findComponentById(ResourceTable.Id_d1);
                d1.setText((EasyProtectorLib.checkIsDebug(this) || BuildConfig.DEBUG) ? "debuggable" : "release");
                break;
            case ResourceTable.Id_checkDebuggerAttach:
                checkIsUsbCharging();
                break;
            case ResourceTable.Id_checkTracer:
                EasyProtectorLib.checkIsBeingTracedByC();
                Text d3 = (Text) findComponentById(ResourceTable.Id_d3);
                d3.setText("see log");
                break;
            case ResourceTable.Id_checkXP:
                Text x1 = (Text) findComponentById(ResourceTable.Id_x1);
                x1.setText(EasyProtectorLib.checkIsXposedExist() ? "shutdown xp success" : "failed");
                break;
            case ResourceTable.Id_readSysProperty:
                checkIsRunningInEmulator();
                break;
            default:
        }
    }

    private void checkByPrivateFilePath() {
        Text v1 = (Text) findComponentById(ResourceTable.Id_v1);
        v1.setText(VirtualApkCheckUtil.getSingleInstance()
            .checkByPrivateFilePath(this, null) ? "privatePath-NO" : "privatePath-OK");
    }

    private void checkByOriginApkPackageName() {
        Text v2 = (Text) findComponentById(ResourceTable.Id_v2);
        v2.setText(VirtualApkCheckUtil.getSingleInstance()
            .checkByOriginApkPackageName(this, null) ? "packageName-NO" : "packageName-OK");
    }

    private void checkByMultiApkPackageName() {
        Text v3 = (Text) findComponentById(ResourceTable.Id_v3);
        v3.setText(VirtualApkCheckUtil.getSingleInstance()
            .checkByMultiApkPackageName(null) ? "maps-NO" : "maps-OK");
    }

    private void checkByHasSameUid() {
        Text v4 = (Text) findComponentById(ResourceTable.Id_v4);
        v4.setText(VirtualApkCheckUtil.getSingleInstance().checkByHasSameUid(null) ? "uid-NO" : "uid-OK");
    }

    private void checkIsRunningInVirtualApk() {
        Text v6 = (Text) findComponentById(ResourceTable.Id_v6);
        v6.setText(EasyProtectorLib
            .checkIsRunningInVirtualApk(getBundleName(), null) ? "LocalServerSocket-NO"
            : "LocalServerSocket-OK");
    }

    private void checkIsUsbCharging() {
        Text d2 = (Text) findComponentById(ResourceTable.Id_d2);
        SecurityCheckUtil.getSingleInstance().checkIsUsbCharging(new Callback<Boolean>() {
            @Override
            public void callback(Boolean isCharge) {
                Log.e(TAG, "b=" + isCharge);
                if (isCharge) {
                    if (SecurityCheckUtil.getSingleInstance().checkIsDebuggerConnected()) {
                        d2.setText("debugger-connect！！");
                    } else {
                        d2.setText("only-usb-charging");
                    }
                } else {
                    d2.setText("only-charging");
                }
            }
        });
    }

    private void checkIsRunningInEmulator() {
        Text e1 = (Text) findComponentById(ResourceTable.Id_e1);
        EasyProtectorLib.checkIsRunningInEmulator(this, new EmulatorCheckCallback() {
            @Override
            public void findEmulator(String emulatorInfo) {
                e1.setText(emulatorInfo);
            }
        });
    }

    @Override
    public void onActive() {
        super.onActive();
    }

    @Override
    public void onForeground(Intent intent) {
        super.onForeground(intent);
    }
}
